const { VM } = require('vm2');

// 模拟冒险岛的全局对象
class NPC {
  constructor(npcId, player, send) {
    this.npcId = npcId;
    this.player = player;
    this.send = send; // 用于向前端发送消息
  }

  askMenu(content) {
    this.send({
      type: 'menu',
      content,
      waitFor: 'choice'
    });
    // 阻塞等待前端返回选择（我们稍后处理）
    return this.waitForInput();
  }

  askNumber(content, defaultVal, min, max) {
    this.send({
      type: 'number',
      content,
      default: defaultVal,
      min, max,
      waitFor: 'number'
    });
    return this.waitForInput();
  }

  askYesNo(content) {
    this.send({
      type: 'confirm',
      content,
      waitFor: 'yesno'
    });
    return this.waitForInput() === 'yes';
  }

  say(content) {
    this.send({ type: 'say', content });
  }

  // 模拟等待用户输入（实际通过状态机实现）
  waitForInput() {
    throw new Error('PAUSE_SCRIPT_EXECUTION'); // 触发暂停
  }
}

// 模拟玩家对象
class Player {
  constructor(data) {
    Object.assign(this, data);
  }

  getLevel() { return this.level; }
  getMeso() { return this.meso; }
  getAmountOfItem(id) { return this.items[id] || 0; }
  canGainItem(id, count) { return true; } // 简化
  gainMeso(amount) { this.meso += amount; }
  loseItem(id, count) { 
    if (this.items[id] >= count) this.items[id] -= count; 
  }
  gainItem(id, count) {
    this.items[id] = (this.items[id] || 0) + count;
  }
  addEventValue(key, type, value, resetHours) {
    this.events = this.events || {};
    this.events[key] = (this.events[key] || 0) + value;
  }
  worldSpouseMessage(type, msg) {
    console.log('[广播]', msg);
  }
  getName() { return this.name; }
  getItemName(id) { return `物品${id}`; } // 可对接数据库
}

module.exports = { NPC, Player };